/**
 * 
 */
$(initproj(), inittalking());
$(initTask());
function initproj() {
	$.ajax({
		type : "post",
		url : "GetCoopListServlet",
		data : {
			projID : projID
		},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			var pro = jsondata.coop_project[0];
			teamID=pro.Team_id;
			$("#projName_node").text(pro.Name);
			$("#projdesc_node").text(pro.Desc);
			$("#aActor").attr("href", "#");
			$("#aSet").attr("href",
					"../communication/coopupdate.html?pid=" + projID + "");
			$("#alltalking").attr("href",
					"../communication/CoopAlltalk.html?pid=" + projID + "");
			$("#aActor").attr("href","../communication/coopUserMa.html?teamID="+teamID+"");
			$("#aStatistics").attr("href","../communication/coopstatistics.html?projID="+projID+"&teamID="+teamID+"");
			$("#adonetask").attr("href","../communication/coopdoneTask.html?projID="+projID);

		},
		error : function() {
			// TODO do something
		}
	});
}
function inittalking() {
	$
			.ajax({
				type : "post",
				url : "GetCoopTalkServlet",
				data : {
					projID : projID,
					state : "进行",
					talk_id : ""
				},
				dateType : "json",
				success : function(data) {
					var jsondata = eval('(' + data + ')');
					var messNode = $("#talkcont");
					messNode.children().remove();
					var pareNode, talkTools;
					$
							.each(
									jsondata.talkArray,
									function(n, v) {
										if (v.State == "结束") {
											return false;
										}
										pareNode = $("<div></div>").addClass(
												"message");
										// 讨论管理工具
										talkTools = addtalktools(v);
										pareNode.append(talkTools);
										pareNode
												.append("<a href=\"#\" target=\"_blank\" title=\"\"><img alt=\"\""
														+ "class=\"avatar\" src=\"../userIcons/"
														+ v.userIcon
														+ "\"></a>"
														+ "<div class=\"name\">"
														+ "<a href=\"#\" data-stack-root=\"true\" data-stack=\"true\" title=\"\">"
														+ v.userNickName
														+ "</a>"
														+ "</div>"
														+ "<a data-stack=\"\" href=\"../communication/cooptalkc.html?talk={tid:"
														+ v.ID
														+ "}\" class=\"message-link\"> "
														+ "<span class=\"message-title\"><span class=\"document-rest\" title=\""
														+ v.Title
														+ "\">"
														+ v.Title
														+ "</span></span> "
														+ "<span class=\"message-content\"> "
														+ v.Body
														+ " </span>"
														+ "</a> <span class=\"time\" title=\""
														+ v.CreatedOn
														+ "\""
														+ "data-readable-time=\"2015-05-14T19:56:19+08:00\">"
														+ v.CreatedOn
														+ "</span>"
														+ "<div class=\"comment-info\">"
														+ "</div>");
										messNode.append(pareNode);
									});
				},
				error : function() {
					// TODO do something
				}
			});
}
function addtalktools(obj) {
	var parentsNode = $("<div></div>").addClass("message-actions actions")
			.attr("data-visible-to", "member");
	var contentdivNode = $("<div></div>").addClass("inr");
	var setTopNode = $("<a href=\"javascript:;\" onclick=\"sharetoBBS('"
			+ obj.Title
			+ "','"
			+ obj.Body
			+ "','"
			+ obj.Creator
			+ "')\" class=\"stick\" title=\"分享至论坛\" data-method=\"post\" data-remote=\"true\" data-loading=\"true\"> <i class=\"twr twr-arrow-circle-up\"></i></a>");
	var shareNode = $("<a href=\"javascript:;\"  onclick=\"updateTalkstate('"
			+ obj.ID
			+ "','结束')\" class=\"archive\" title=\"取消讨论\" data-method=\"post\" data-remote=\"true\" data-loading=\"true\"> <i class=\"twr twr-archive-custom\"></i></a>");
	contentdivNode.append(setTopNode);
	contentdivNode.append(shareNode);
	parentsNode.append(contentdivNode);
	return parentsNode;
}

function addtalking() {
	var edittalkNode;
	edittalkNode = $("<div class=\"form-item\"><div class=\"form-field\" style=\"margin-bottom: 13px; margin-top: 13px;\">"
			+ "<input id=\"txt-title\" type=\"text\" data-autosave=\"project-9e3709e48ef542578b3c16b8920bb1fa-new-message-title\" data-validate-msg=\"话题最长255个字符\" data-validate=\"length:0,255\" placeholder=\"话题\" name=\"subject\" tabindex=\"1\">"
			+ "</div></div>"
			+ "<div class=\"form-item\"><textarea id=\"edit_area\" name=\"edit_area\" rows=\"20\" style=\"width: 99.4%;height:200px\"></textarea> </div>"
			+ "<div class=\"form-item notify\"><div class=\"notify-title\"></div></div>"
			+ "<div class=\"form-buttons\"  style=\"margin-top: 13px; margin-bottom: 20px;\">"
			+ "<button id=\"btn-post\" class=\"btn btn-primary\" data-disable-with=\"正在提交...\" onclick=\"postTalk()\" tabindex=\"1\">发起讨论</button>"
			+ "<a id=\"link-cancel-post\" class=\"btn btn-x\" onclick=\"removeNodechildren('editNode')\" href=\"javascript:;\" tabindex=\"2\">取消</a>"
			+ "</div>");
	$("#editNode").append(edittalkNode);
	$(function() {
		edittalkNode
				.find("#edit_area")
				.xheditor(
						{
							plugins : getCodePlugin(),
							tools : "Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Emot,Table,Code,|,Source,Fullscreen",
							upImgUrl : "!../learning/uploadimg.jsp",
							loadCSS : "../css/editor.css"
						});
	});
}
function removeNodechildren(id) {
	$("#" + id).children().remove();
}
function postTalk() {
	var title = $("#txt-title").val();
	var body = $("#edit_area").val();
	$.ajax({
		type : "post",
		url : "PutNewTalkServlet",
		data : {
			projID : projID,
			title : title,
			body : body,
			user_id : user_id
		},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if (jsondata.isSuccess == "true") {
				if (confirm("发表 成功!")) {
					inittalking();
					removeNodechildren('editNode');
				}
			} else {
				confirm("发表 失败！请重试...");
			}
		},
		error : function() {
			// TODO do something
		}
	});
}
function updateTalkstate(t_id, tyle) {
	$.ajax({
		type : "post",
		url : "UpdateCoopTalkStateServlet",
		data : {
			t_id : t_id,
			tyle : tyle
		},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if (jsondata.isSuccess == "true") {
				inittalking();
			} else {
				confirm("操作 失败！请重试...");
			}
		},
		error : function() {
			// TODO do something
		}
	});
}
function sharetoBBS(textTitle, textArea, uid) {
	$.ajax({
		type : "post",
		url : "../community/PutTopicServlet",
		data : {
			labelID : '0',
			textTitle : textTitle,
			textArea : textArea,
			c_id : '5',
			user_id : uid
		},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if (jsondata.isSuccess == "0") {
				confirm("分享 成功！请重试...");
			} else {
				confirm("分享 失败！ 版主偷懒咯~~~");
			}
		},
		error : function() {
			// TODO do something

		}
	});
}
function initTask() {
	$
			.ajax({
				type : "post",
				url : "GetCoopTaskServlet",
				data : {
					projID : projID,
					tyle : '',
					task_id:''
				},
				dateType : "json",
				success : function(data) {
					var jsondata = eval('(' + data + ')');
					var tasknode;
					tasknode = $("#taskulnode");
					tasknode.children().remove();
					$
							.each(
									jsondata.taskArray,
									function(n, v) {
										if(v.State=="结束"){
											return false;
										}
										tasknode
												.append("<li class=\"todo running\">"
														+ "<div class=\"todo-wrap\">"
														+ "<div class=\"simple-checkbox\""
														+ "style=\"height: 16px; width: 16px;\"></div>"
														+ "<span class=\"runner on\" title=\"\"> <img alt=\"\""
														+ "class=\"avatar\" src=\"../userIcons/"
														+ v.eIcon
														+ "\">"
														+ "</span> <span class=\"content-non-linkable\"> </span> <span"
														+ "class=\"content-linkable\"> <a href=\"../communication/cooptaskc.html?task_id="+v.ID+"\""
														+ "class=\"todo-rest\" data-stack=\"true\" title=\""
														+ v.Name
														+ "\">"
														+ v.Name
														+ "</a>"
														+ "</span> <a class=\"label todo-assign-due delay\" href=\"../communication/cooptaskc.html?task_id="+v.ID+"\""
														+ "data-request-members=\"\"> <span class=\"assignee\""
														+ "data-guid=\"\" data-gavatar=\"\"> "
														+ v.eName
														+ " </span> <span"
														+ "class=\"due\" data-date=\"\">计划  "
														+ v.etime
														+ "</span></a></div></li>");
									});
				},
				error : function() {
					// TODO do something

				}
			});
}
function newTaskNode() {
	if($("#taskulnode").children("#newTaskliNode").length!=0){
		return false;
	}
	
	$("#taskulnode")
			.append(
					"<li id=\"newTaskliNode\" class=\"todo-form new\">"
							+ "<div class=\"form form-edit-todo\" style=\"margin-top: 20px;\" data-remote=\"true\">"
							+ "<div class=\"form-body\">"
							+ "<textarea id=\"todo_content\" class=\"todo-content no-border\""
							+ " data-validate-msg=\"\" data-validate=\"custom\""
							+ " placeholder=\"新的任务\" name=\"todo_content\""
							+ " data-autosave=\"list-undefined-new-todo\""
							+ " style=\"overflow: hidden; word-wrap: break-word; resize: none; height: 24px;\"></textarea></div>"
							+"<select id=\"chooseActor\" class=\"invite-subgroup\" style=\"margin-top: 10px;\" name=\"\">"
							+"<option value=\"0\">成员</option><option disabled=\"\">-----</option>"
							+"</select>"
							+"<input id=\"datetimepicker\" type=\"text\" style=\"padding-top: 5px; padding-bottom: 5px; width: 120px; margin-top: 10px;\">"
							+ "<div class=\"buttons create-buttons\">"
							+ "<button class=\"btn btn-primary btn-create-todo\""
							+ " data-disable-with=\"正在保存...\" onclick=\"postNewTask()\" >添加任务</button>"
							+ "<a class=\"btn-cancel-todo\" onclick=\"hideNewTaskNode()\" href=\"javascript:;\">取消</a>"
							+ "</div></div></li>");
	$.ajax({
		type : "post",
		url : "GetTeamActorServlet",
		data : {
			teamID:teamID
		},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			$.each(jsondata.tUserArray,function (n,v){
				var node=$("#chooseActor");
				node.append("<option value=\""+v.userId+"\">"+v.userNickName+"</option>");
			});
		},
		error : function() {
			// TODO do something

		}
	});
	$("#datetimepicker").datetimepicker({lang:'ch'});
}
function hideNewTaskNode(){
	$("#newTaskliNode").remove();
}
function postNewTask() {
	var name=$("#todo_content").val();
	var eid=$("#chooseActor").val();
	var etime=$("#datetimepicker").val();
	$.ajax({
		type : "post",
		url : "PutCoopTaskServlet",
		data : {
			user_id:user_id,name:name,eid:eid,etime:etime,projID:projID
		},
		dateType : "json",
		success : function(data) {
			var jsondata = eval('(' + data + ')');
			if(jsondata.isSuccess=="true"){
				if (confirm("创建 成功!")) {
					initTask();
				}
			}else{
				confirm("创建失败 ! 请重试...");
			}
		},
		error : function() {
			// TODO do something

		}
	});
}